<?php

namespace iBrand\Shop\Server\Controllers\V2;

use Illuminate\Http\Request;
use Storage;

class UploadController extends Controller
{
	public function upload(Request $request)
	{
		$key = $request->input('key');
		if (!$request->hasFile($key)) {
			return $this->failed('请上传图片');
		}

		$file = $request->file($key);
		$ext  = $file->getClientOriginalExtension();
		if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) {
			return $this->failed('不允许上图的图片类型');
		}

		$name = md5(uniqid()) . '.' . $ext;
		$path = $file->storeAs(
			'upload/image', $name, 'public'
		);

		$fileUrl = Storage::disk('public')->url($path);

		return $this->success(['url' => $fileUrl]);
	}
}